package com.sinochem.yunlian.ship.base.rest.app;

import com.alibaba.fastjson.JSON;
import com.sinochem.yunlian.ship.base.biz.BsAppBannerBiz;
import com.sinochem.yunlian.ship.base.entity.BsAppBanner;
import com.sinochem.yunlian.ship.base.vo.request.BsBannerVo;
import com.sinochem.yunlian.ship.base.vo.request.FindAppBannerVo;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import com.sinochem.yunlian.ship.common.rest.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${pc} on 2018/1/27
 */
@RestController
@RequestMapping(value = {"/anon/m/appBanner","appBanner"})
@Slf4j
public class BsAppBannerController extends BaseController<BsAppBannerBiz,BsAppBanner> {

    @Autowired
    private BsAppBannerBiz bsAppBannerBiz;


    @RequestMapping(value = "/getList",method = RequestMethod.POST)
    public RestResponse getBannerList(){
        List<BsAppBanner> BannerList = bsAppBannerBiz.getBannerList();
        List<FindAppBannerVo> resultList =  new ArrayList<FindAppBannerVo>();
        if (BannerList!=null){
            for (BsAppBanner bsAppBanner : BannerList) {
                FindAppBannerVo vo = new FindAppBannerVo();
                vo.setTargetUrl(bsAppBanner.getTargetUrl());
                vo.setTitle(bsAppBanner.getTitle());
                vo.setImageUrl(bsAppBanner.getImageUrl());
                resultList.add(vo);
            }
        }
        return RestResponse.success(resultList);
    }

    /**
     * banner添加、修改
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public RestResponse bannerAdd(@RequestBody BsBannerVo bannerVo){
        try {
            log.info("base banner add bannerVo:{}", JSON.toJSONString(bannerVo));
            if(bannerVo.getType()==null){
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }
            BsAppBanner banner = new BsAppBanner();
            BeanUtils.copyProperties(bannerVo, banner);
            if(bannerVo.getId()==null){
                banner.setCreateBy(bannerVo.getUcUserInfo().getId());
                bsAppBannerBiz.addBanner(banner);
            }else{
                banner.setUpdateBy(bannerVo.getUcUserInfo().getId());
                bsAppBannerBiz.updateBanner(banner);
            }
            return RestResponse.success();
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return new RestResponse(ResponseCode.SYS_EXCEPTION);
        }
    }

    /**
     * banner删除
     * @return
     */
    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    public RestResponse bannerRemove(@RequestBody BsBannerVo bannerVo){
        try {
            log.info("base banner bannerRemove bannerVo:{}", JSON.toJSONString(bannerVo));
            if(bannerVo.getId()==null){
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }

            bsAppBannerBiz.deleteBanner(bannerVo);
            return RestResponse.success();
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return new RestResponse(ResponseCode.SYS_EXCEPTION);
        }
    }

    /**
     * banner 列表
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public RestResponse getBannerList(@RequestBody BsBannerVo bannerVo){
        try {
            log.info("base banner getBannerList bannerVo:{}", JSON.toJSONString(bannerVo));
            if(bannerVo.getType()==null){
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }
            return RestResponse.success( bsAppBannerBiz.getBannerList(bannerVo));
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return new RestResponse(ResponseCode.SYS_EXCEPTION);
        }
    }


    /**
     * banner 移动
     * @return
     */
    @RequestMapping(value = "/move",method = RequestMethod.POST)
    public RestResponse moveBanner(@RequestBody BsBannerVo bannerVo){
        try {
            log.info("base banner moveBanner bannerVo:{}", JSON.toJSONString(bannerVo));
            if(bannerVo.getId()==null){
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }

            if(StringUtils.isBlank(bannerVo.getDirectionFlag())){
                return new RestResponse(ResponseCode.INVALID_PARAM);
            }
            return bsAppBannerBiz.moveBanner(bannerVo);
        }catch(Exception e){
            log.error(e.getMessage(), e);
            return new RestResponse(ResponseCode.SYS_EXCEPTION);
        }
    }

}
